package com.base.game.graphics;

import java.util.ArrayList;

import org.newdawn.slick.opengl.Texture;

import com.base.engine.graphics.TexUV;
import com.base.game.terrain.EBlockType;

public abstract class TextureManager {
	
	public static final float[] U = {0,1,1,0} ; 
	public static final float[] V = {1,1,0,0} ; 

	public static final float[][] _U = {U,U,U,U,U,U} ; 
	public static final float[][] _V = {V,V,V,V,V,V} ; 
	
	public static final Texture ENVIRONMENT = GameTextureLoader.loadEnvTexture("environment.png") ;
	public static final Texture SKYBOX = GameTextureLoader.loadSkyTexture("skybox2.png"); 
	public static final Texture WATER_REFLECT = GameTextureLoader.loadWaterTexture("reflect.png"); 
	public static final Texture WATER_REFRACT  = GameTextureLoader.loadWaterTexture("refract.jpg") ; 
	public static final Texture WATER_BUMP = GameTextureLoader.loadWaterTexture("bump.jpg") ; 
	
	// default blocks 
	private static final TexUV[] T_GRASS_TOP = getTexCoords(ENVIRONMENT,16,1,0) ;  
	private static final TexUV[] T_GRASS_SIDE = getTexCoords(ENVIRONMENT,16,2,0); 
	private static final TexUV[] T_GRASS_BOTTOM = getTexCoords(ENVIRONMENT,16,0,0); 
	private static final TexUV[] T_DIRT = getTexCoords(ENVIRONMENT,16,0,0); 
	private static final TexUV[] T_WATER = getTexCoords(ENVIRONMENT,16,3,1); 
	private static final TexUV[] T_STONE = getTexCoords(ENVIRONMENT,16,3,0); 
	private static final TexUV[] T_OAKWOOD_TOP = getTexCoords(ENVIRONMENT,16,0,1); 
	private static final TexUV[] T_OAKWOOD_SIDE = getTexCoords(ENVIRONMENT,16,1,1); 
	private static final TexUV[] T_SPRUCEWOOD_TOP = getTexCoords(ENVIRONMENT,16,0,1); 
	private static final TexUV[] T_SPRUCEWOOD_SIDE = getTexCoords(ENVIRONMENT,16,5,2); 
	private static final TexUV[] T_SAND = getTexCoords(ENVIRONMENT,16,4,0);
	private static final TexUV[] T_OAKLEAVES = getTexCoords(ENVIRONMENT,16,2,1); 
	private static final TexUV[] T_SPRUCELEAVES = getTexCoords(ENVIRONMENT,16,4,2); 
	private static final TexUV[] T_COAL = getTexCoords(ENVIRONMENT,16,0,2); 
	private static final TexUV[] T_IRON = getTexCoords(ENVIRONMENT,16,1,2); 
	private static final TexUV[] T_GOLD = getTexCoords(ENVIRONMENT,16,2,2); 
	private static final TexUV[] T_DIAMOND = getTexCoords(ENVIRONMENT,16,3,2); 
	
	// plants 
	private static final TexUV[] T_P_GRASS = getTexCoords(ENVIRONMENT,16,5,1); 
	private static final TexUV[] T_P_FLOWER_YELLOW = getTexCoords(ENVIRONMENT,16,6,0); 
	private static final TexUV[] T_P_FLOWER_RED= getTexCoords(ENVIRONMENT,16,5,0); 	
	
	// skybox 
	private static final TexUV[] T_SKY_SOUTH = getTexCoords(SKYBOX,4,0,1); 
	private static final TexUV[] T_SKY_WEST = getTexCoords(SKYBOX,4,1,1); 
	private static final TexUV[] T_SKY_NORTH = getTexCoords(SKYBOX,4,2,1); 
	private static final TexUV[] T_SKY_EAST = getTexCoords(SKYBOX,4,3,1); 
	private static final TexUV[] T_SKY_TOP = getTexCoords(SKYBOX,4,1,0); 
	private static final TexUV[] T_SKY_BOTTOM = getTexCoords(SKYBOX,4,1,2); 
	
	// Water 
	private static final TexUV[] T_WATER_REFLECT = getTexCoords(WATER_REFLECT,1,0,0); 
	private static final TexUV[] T_WATER_REFRACT = getTexCoords(WATER_REFRACT,1,0,0); 
	
	private static final float TEX_COR_VALUE = 0.0002f ;
	
	private static TexUV[] getTexCoords(Texture tex, int amt, int col, int row){
		float w = tex.getImageWidth() ; 
		float h = w ; 
		float i1 = w / amt ; 
		TexUV t1 = new TexUV((col*i1)/w+TEX_COR_VALUE, (row*i1+i1)/h-TEX_COR_VALUE) ; 
		TexUV t2 = new TexUV((col*i1+i1)/w-TEX_COR_VALUE, (row*i1+i1)/h-TEX_COR_VALUE) ; 
		TexUV t3 = new TexUV((col*i1+i1)/w-TEX_COR_VALUE, (row*i1)/h+TEX_COR_VALUE) ; 
		TexUV t4 = new TexUV((col*i1)/w+TEX_COR_VALUE, (row*i1)/h+TEX_COR_VALUE) ;
		return new TexUV[] {t1, t2, t3, t4} ; 
	}
	
	public static ArrayList<TexUV[]> getTexCoordsByBlockType(EBlockType type){
		ArrayList<TexUV[]> tmp = new ArrayList<TexUV[]>(); 
		if(!EBlockType.isPlant(type)){
			switch(type){
			case GRASS:  for(int i=0; i<4; i++) tmp.add(T_GRASS_SIDE); 
							tmp.add(T_GRASS_BOTTOM); 
							tmp.add(T_GRASS_TOP); 
							break ; 
			case DIRT: for(int i=0; i<6; i++) tmp.add(T_DIRT); 
							break ;
			case WATER: for(int i=0; i<6; i++) tmp.add(T_WATER); 
							break ;
			case STONE: for(int i=0; i<6; i++) tmp.add(T_STONE); 
							break; 
			case OAK_WOOD: for(int i=0; i<4; i++) tmp.add(T_OAKWOOD_SIDE); 
						for(int i=0; i<2; i++) tmp.add(T_OAKWOOD_TOP); 
							break ; 
			case SPRUCE_WOOD: for(int i=0; i<4; i++) tmp.add(T_SPRUCEWOOD_SIDE); 
						for(int i=0; i<2; i++) tmp.add(T_SPRUCEWOOD_TOP); 
							break ; 
			case SAND: for(int i=0; i<6; i++) tmp.add(T_SAND); 
			case OAK_LEAVES: for(int i=0; i<6; i++) tmp.add(T_OAKLEAVES); 	
			case SPRUCE_LEAVES: for(int i=0; i<6; i++) tmp.add(T_SPRUCELEAVES); 	
			case COAL: for(int i=0; i<6; i++) tmp.add(T_COAL); 
							break ; 
			case IRON: for(int i=0; i<6; i++) tmp.add(T_IRON); 
			case GOLD: for(int i=0; i<6; i++) tmp.add(T_GOLD); 
			case DIAMOND: for(int i=0; i<6; i++) tmp.add(T_DIAMOND); 
			}
		// plants are textured here 
		}else{
			switch(type){
			case PLANT_GRASS: tmp.add(T_P_GRASS) ; 
					break ; 
			case PLANT_FLOWER_YELLOW: tmp.add(T_P_FLOWER_YELLOW) ; 
				break ; 
			case PLANT_FLOWER_RED: tmp.add(T_P_FLOWER_RED) ; 
				break ; 
			}
		}
		return tmp ; 
	}
	
	public static ArrayList<TexUV[]> getSkyBoxTexCoords(){
		ArrayList<TexUV[]> tmp = new ArrayList<TexUV[]>(); 
		tmp.add(T_SKY_SOUTH); 
		tmp.add(T_SKY_NORTH); 
		tmp.add(T_SKY_EAST); 
		tmp.add(T_SKY_WEST); 
		tmp.add(T_SKY_BOTTOM); 
		tmp.add(T_SKY_TOP);
		return tmp ; 
	}
	
	public static ArrayList<TexUV[]> getWaterTexCoords(){
		ArrayList<TexUV[]> tmp = new ArrayList<TexUV[]>(); 
		tmp.add(T_WATER_REFLECT) ; 
		tmp.add(T_WATER_REFRACT) ; 
		return tmp ; 
	}
	
}
